"use strict";

function init() {
    var vm = new Vue({
        el: "#app",
        data: {
            formData: {
                principal:'',
                password:'',
            },

            isActive: false,
            isUp: false,
            hideLoadingToast: true,
            hideErrorAlert: true,
            principalPlaceHolder: "用户名/手机号/邮箱",
            passwordPlaceHolder: "输入密码"
        },
        methods: {
            focus(type) {
                if(type == 0) {
                    this.principalPlaceHolder = ""
                } else if(type == 1) {
                    this.passwordPlaceHolder = ""
                }
                this.isUp = true;
            },
            blur(type) {
                if(type == 0) {
                    this.principalPlaceHolder = "用户名/手机号/邮箱"
                } else if(type == 1) {
                    this.passwordPlaceHolder = "输入密码"
                }
                this.isUp = false;
            },
            fieldChange() {
                if(this.formData.principal && this.formData.password) {
                    this.isActive = true
                } else {
                    this.isActive = false
                }
            },
            submit() {
                if(!this.isActive)  return;
                var _this = this;
                this.hideLoadingToast = false
                userService.login(this.formData, function(data) {
                    mm.setStorage($C.accessToken, data);
                    // 获取用户信息，存储到本地
                    userService.getUserInfo(function(data) {
                        mm.setStorage($C.currUser, data);
                        _this.hideLoadingToast = true
                        _this.success()
                    }, function(errMsg) {
                        _this.hideLoadingToast = true
                        _this.hideErrorAlert = false
                    })
                }, function(errMsg) {
                    _this.hideLoadingToast = true
                    _this.hideErrorAlert = false
                });
            },
            success() {
                mm.go("../main/main.html")
            },
            hideAlert() {
                this.hideErrorAlert = true
            },
            register() {
                mm.go("../register/register.html?backUrl=../login/login.html")
            }
        }
    })
}

init();